<?php
/*
*author:TT
*createtime:2017/3/7 18:56
*description:
*/
namespace haibao\jiepai\business;

class Album extends Base
{
    /**
     * @var $_instance
     */
    private static $_instance;

    /**
     * instance()
     */
    private static function instance()
    {
        if (!self::$_instance) {
            self::$_instance = new \haibao\jiepai\data\Album();
        }
        return self::$_instance;
    }

    public static function getMyAlbumList($userId)
    {
        return self::instance()->getAlbumList($userId, true);
    }

    public static function getAlbumById($albumId)
    {
        $albumId = intval($albumId);
        return self::instance()->getAlbumById($albumId);
    }

    /**
     * 取首页热门相册列表
     * @param null $cursor
     * @return array
     */
    public static function getHotAlbumList($cursor = null)
    {
        $albumList = self::instance()->getHotAlbumList($cursor);
        $imageIds = array();
        if ($albumList) {
            foreach ($albumList as $key => $album) {
                $imageIds = array_merge($imageIds, $album['Pictures']);
            }
        }
        if ($imageIds) {
            $newestPicture = self::instance()->getNewestPictureByIds($imageIds);
        }
        if ($albumList) {
            foreach ($albumList as $key => $album) {
                if (!$album['Pictures']) continue;
                $tmp = array();
                foreach ($album['Pictures'] as $k => $v) {
                    unset($albumList[$key]['Pictures'][$k]);
                    if (!isset($newestPicture[$v])) continue;
                    array_push($tmp, $newestPicture[$v]);
                }
                $albumList[$key]['Pictures'] = $tmp;
            }
        }
        return $albumList;
    }

    /**
     * 根据相册ID取相册内图片列表
     */
    public static function getPictureByAlbumId($userId, $albumId, $cursor = null)
    {
        $albumId = intval($albumId);
        if (!$albumId) {
            return false;
        }
        $albumInfo = self::instance()->GetAlbums($albumId);
        $albumInfo['Avatar'] = '';
        unset($albumInfo['Flag'], $albumInfo['ViewNum'], $albumInfo['CreateTime']);
        $picList = self::instance()->getPictureByAlbumId($albumId, $cursor);

        if ($picList) {
            $pictureIds = array();
            foreach ($picList as $key => &$picture) {
                $picture['ContentID'] = intval($picture['ContentID']);
                $picture['FileID'] = intval($picture['FileID']);
                unset($picture['Id'], $picture['UserID'], $picture['CropX'], $picture['CropY'], $picture['CropWidth'], $picture['CropHeight'], $picture['Ratio']);
                array_push($pictureIds, $picture['FileID']);
            }

            // 查询用户是否收藏图片和点赞数量
            $collectResult = \haibao\jiepai\business\Picture::getIsCollect($userId, $pictureIds);
            $praiseResult = \haibao\jiepai\business\Picture::getIsPraise($userId, $pictureIds);

            // 查询图片评论数量
            $commentResult = \haibao\jiepai\business\Comment::getCommentCountByImageIds($pictureIds);

            // 查询图片收藏来源文章
            $sourceResult = \haibao\jiepai\business\Picture::getCollectSource($pictureIds);

            //SourceType （收藏来源类型 1文章或私照2） SourceTypeID （文章ID或人物ID）
            $sourceIdResult = \haibao\jiepai\business\Picture::getCollectSourceById($pictureIds);
            foreach ($picList as $key => &$picture) {
                $picture['IsPictureCollect'] = isset($collectResult[$picture['FileID']]) ? $collectResult[$picture['FileID']] : 0;
                $picture['IsPicturePraise'] = isset($praiseResult[$picture['FileID']]) ? $praiseResult[$picture['FileID']] : 0;
                $picture['CommentCount'] = isset($commentResult[$picture['FileID']]) ? $commentResult[$picture['FileID']] : 0;
                $picture['CollectSource'] = isset($sourceResult[$picture['FileID']]) ? $sourceResult[$picture['FileID']] : '';
                $picture['SourceType'] = isset($sourceIdResult[$picture['FileID']]) ? $sourceIdResult[$picture['FileID']]['SourceType'] : 0;
                $picture['SourceTypeID'] = isset($sourceIdResult[$picture['FileID']]) ? $sourceIdResult[$picture['FileID']]['SourceTypeID'] : 0;
            }
        }
        return array(
            'albumInfo' => $albumInfo,
            'list' => $picList
        );
    }

    /**
     * 获取用户相册信息
     */
    public static function getUserAlbumInfo($userId)
    {
        if (!$userId = intval($userId)) {
            return array();
        }
        return self::instance()->getAlbumInfoByUserId($userId);
    }

    /**
     * 相冊更多列表【相册模式】
     */
    public static function moreListByAlbum($userId)
    {
        if (!$userId = intval($userId)) {
            return array();
        }
        return self::instance()->getAlbumList($userId);
    }

    /**
     * 相冊更多列表【图片模式】
     */
    public static function moreListByPicture($userId, $albumUserId, $cursor = null)
    {
        if (!$albumUserId = intval($albumUserId)) {
            return array();
        }
        $imageList = self::instance()->getImageList($albumUserId, $cursor);

        $imageIds = array();
        $list = array();
        $cursor = null;
        $count = 0;
        if ($imageList) {
            foreach ($imageList as $key => &$image) {
                array_push($imageIds, $image['FileID']);
                $image['FileID'] = intval($image['FileID']);
                $image['AlbumID'] = intval($image['AlbumID']);
                unset($image['CropX'], $image['CropY'], $image['CropWidth'], $image['CropHeight'], $image['Ratio']);
                if (isset($image['Size'])) {
                    unset($image['Size']);
                }
            }

            // 查询用户是否收藏图片和点赞数量
            $collectResult = \haibao\jiepai\business\Picture::getIsCollect($userId, $imageIds);
            $praiseResult = \haibao\jiepai\business\Picture::getIsPraise($userId, $imageIds);

            // 查询图片评论数量
            $commentResult = \haibao\jiepai\business\Comment::getCommentCountByImageIds($imageIds);

            // 查询图片收藏来源文章
            $sourceResult = \haibao\jiepai\business\Picture::getCollectSource($imageIds);

            //SourceType （收藏来源类型 1文章或私照2） SourceTypeID （文章ID或人物ID）
            $sourceIdResult = \haibao\jiepai\business\Picture::getCollectSourceById($imageIds);
            foreach ($imageList as $key => &$item) {
                $item['IsPictureCollect'] = isset($collectResult[$item['FileID']]) ? $collectResult[$item['FileID']] : 0;
                $item['IsPicturePraise'] = isset($praiseResult[$item['FileID']]) ? $praiseResult[$item['FileID']] : 0;
                $item['CommentCount'] = isset($commentResult[$item['FileID']]) ? $commentResult[$item['FileID']] : 0;
                $item['CollectSource'] = isset($sourceResult[$item['FileID']]) ? $sourceResult[$item['FileID']] : '';

                $item['SourceType'] = isset($sourceIdResult[$item['FileID']]) ? $sourceIdResult[$item['FileID']]['SourceType'] : 0;
                $item['SourceTypeID'] = isset($sourceIdResult[$item['FileID']]) ? $sourceIdResult[$item['FileID']]['SourceTypeID'] : 0;

                $yearMonth = date('Y', strtotime($item['CreateTime'])) == date('Y') ? ltrim(date('m月', strtotime($item['CreateTime'])), '0') : date('Y年m月', strtotime($item['CreateTime']));
                if (!isset($list[$yearMonth])) {
                    $list[$yearMonth] = array();
                }
                $list[$yearMonth]['YearMonth'] = date('Ym', strtotime($item['CreateTime']));
                array_push($list[$yearMonth], $item);
            }

            $pictureCountGroupByDate = self::instance()->getPictureCountGroupByDate($albumUserId);

            $retArr = array();
            if ($list) {
                foreach ($list as $key => $value) {
                    $date = $value['YearMonth'];
                    unset($value['YearMonth']);
                    array_push($retArr, array(
                        'Title' => $key,
                        'PictureCount' => (isset($pictureCountGroupByDate[$date])) ? intval($pictureCountGroupByDate[$date]) : 0,
                        'PictureInfoList' => $value,
                    ));
                }
            }
            $count = count($imageList);
            if ($count > 0) {
                $cursor = $imageList[$count - 1]['CreateTime'];
            }
        }
        return array(
            'pictureList' => $retArr,
            'nextCursor' => $cursor,
            'count' => $count
        );
    }

    public static function getAlbumsByFilter($filter)
    {

        return self::instance()->getAll($filter);
    }

    /**
     * 置顶/取消置顶相册
     * @param $albumId
     * @param $type 1：置顶，0：取消置顶
     */
    public static function setAlbumToTop($albumId, $type)
    {
        self::instance()->setAlbumToTop(intval($albumId), intval($type));
    }

    public static function deleteHotAlbum($albumId)
    {
        self::instance()->deleteHotAlbum(intval($albumId));
    }

    public static function authAlbumName($userId, $albumName)
    {

        if (!$albumName) return false;
        $album = self::instance()->authAlbumName((int)$userId, $albumName);
        return $album ? true : false;
    }

    public static function CreateNewAlbum($model)
    {
        $albumId = self::instance()->CreateAlbum($model);
        return $albumId;
    }

    public static function CheckAlbumIsDefault($albumId)
    {

        return self::instance()->CheckAlbumIsDefault((int)$albumId);
    }

    public static function getAlbumInfo($albumId, $userId)
    {

        return self::instance()->getAlbumInfo((int)$albumId, (int)$userId);
    }

    public static function delAlbum($albumId, $userId)
    {
        return self::instance()->delAlbum((int)$albumId, (int)$userId);
    }

    public static function updateAlbum($albumName, $userId, $albumId, $isPrivate = 0)
    {
        return self::instance()->updateAlbum($albumName, (int)$userId, (int)$albumId, $isPrivate);
    }

    public static function updateAlbumStatistic($albumId)
    {
        return self::instance()->updateAlbumStatistic((int)$albumId);
    }

    /**
     * 创建默认相册
     */
    public static function createDefaultAlbum($userId)
    {
        $model = new \haibao\jiepai\model\data\user\CmsUserAlbums();
        $model->UserID = $userId;
        $model->AlbumName = '默认相册';
        $model->Type = $model::ALBUM_DEFAULT;
        $model->CreateTime = new \DateTime();
        $model->Flag = $model::FLAG_IS;

        $data = new \haibao\jiepai\data\Album();
        $data->createDefaultAlbum($model);
    }

}
